package com.test;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public class DateUtils {

    public static String dateFormat(Date date) {
        return dateFormat(date,"yyyyMMddHHmmss");
    }

    public static String dateFormat(Date date,String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            String str_time = sdf.format(date);
            return str_time;
        }catch (Exception e){
            log.error("data format error: {}",e);
            return "";
        }
    }


    public static Date dateParse(String str_time){
        return dateParse(str_time,"yyyy-MM-dd HH:mm:ss");
    }

    public static Date dateParse(String str_time,String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = sdf.parse(str_time);
            return date;
        }catch (Exception e){
            log.error("data parse error: {}",e);
            return null;
        }
    }

    /**
     * 日期【按月】加减
     *  count > 0 按月加count个月，如1，就是下一个月
     *  count < 0 按月减count个月，如-1，就是上一个月
     */
    public static Date dateAddMonth(Date date,int count) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(Calendar.MONTH,count);
        Date newDate = gc.getTime();
        return newDate;
    }

    //日期[按天]加减
    public static Date dateAddDay(Date date,int count) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(Calendar.DAY_OF_MONTH,count);
        Date newDate = gc.getTime();
        return newDate;
    }

    //日期操作,按[interval]规定的模式 加减
    public static Date dateOperation(Date date, int interval, int count) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(interval, calendar.get(interval) + count);
        return calendar.getTime();
    }

    /**
     * 获取输入日期 月的每一天
     * @return yyyyMMdd
     */
    public static List<String> getDayByDate(Date date){
        List list = new ArrayList();
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.setTime(date);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH) + 1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String aDate=null;
            if(month<10&&i<10){
                aDate = String.valueOf(year)+"0"+month+"0"+i;
            }
            if(month<10&&i>=10){
                aDate = String.valueOf(year)+"0"+month+""+i;
            }
            if(month>=10&&i<10){
                aDate = String.valueOf(year)+""+month+"0"+i;
            }
            if(month>=10&&i>=10){
                aDate = String.valueOf(year)+""+month+""+i;
            }
            list.add(aDate);
        }
        return list;
    }



    /***
     * @Desc 获取此月份的1号0时0分0秒的时间
     **/
    public static Date get1000OfDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
}
